/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.QuickTips.init();
var _pageSize = 20;

var fieldName = ['id', 'userid', "loginName", "realName", 'actionName', 'detail', 'createTime', 'orgCode', 'orgName'];
var fieldLabel = ['ID', "userID", '用户名', '真实姓名', "执行操作", '详细信息', '创建时间', '机构编码', '所属单位'];
var fieldType = [];

var saveServlet = '../../servlet/system/LogSave';
var loadServlet = '../../servlet/system/LogLoad';
var deleteServlet = '../../servlet/system/LogDel';
var dbtable = 't_log';
var theme = '日志';

var _title = currentOrgName + theme + '管理';

var inputForm = new Ext.FormPanel(
        {
            xtype: 'form',
            id: 'input-form',
            bodyStyle: 'padding:15px;background:transparent',
            frame: true,
            border: false,
            url: saveServlet,
            items: [{
                    name: fieldName[0],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[0],
                    hidden: true
                }, {
                    name: fieldName[1],
                    //   id: fieldName[1],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[1],
                    allowBlank: true
                }, {
                    name: fieldName[2],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[2],
                    width: 150
                }, {
                    name: fieldName[3],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[3],
                    width: 150

                }, {
                    name: fieldName[4],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[4],
                    width: 150
                }, {
                    name: fieldName[5],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[5],
                    width: 150

                }, {
                    id: fieldName[6],
                    name: fieldName[6],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[6],
                    width: 150,
                    readOnly: true
                }
            ],
            buttons: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function() {
                        var submitConfig = {
                            success: function(form, action) {
                                store.reload();
                                win.hide();
                            },
                            failure: function(form, action) {
                                switch (action.failureType) {
                                    case Ext.form.Action.CONNECT_FAILURE:
                                        Ext.Msg.alert('提示', '网络连接失败');
                                        break;
                                    case Ext.form.Action.SERVER_INVALID:
                                        Ext.Msg.alert('提示', action.result.msg);
                                }
                            },
                            params: {}
                        };
                        var token1 = new Date().getTime(); 
                        submitConfig.params.token=token;
                        submitConfig.params.token1=token1;
                        if (type === 1)
                            submitConfig.params.cmd = 'create';
                        else {
                            submitConfig.params.id = checkboxSel.getSelected().get("id");
                            submitConfig.params.cmd = 'update';
                        }
                        Ext.getCmp('input-form').getForm().submit(submitConfig);
                    }
                }, {
                    text: '取消',
                    handler: function() {
                        win.hide();
                    }
                }]
        }
);
var win = new Ext.Window({
    layout: 'form',
    width: 340,
    autoHeight: true,
    closeAction: 'hide',
    items: [inputForm]
});
var type = 1;         //判断打开窗口是进行更新，还是新建。1-新建；2-修改
var updated = false;
function addnew() {
    if (win === null) {
        win = new Ext.Window({
            layout: 'form',
            width: 340,
            autoHeight: true,
            closeAction: 'hide',
            items: [inputForm]
        });
    }
    type = 1;
    win.setTitle('增加' + theme);
    //if (updated)
    // Ext.getCmp('addusr-form').getForm().getEl().dom.reset();
    Ext.getCmp('input-form').getForm().reset();
    //Ext.getCmp(fieldName[6]).setValue(currentOrgCode);
    win.show();
    //loginForm.form.reset();

    //Ext.getCmp('code').enable();    
}

function update() {
    updated = true;
    if (checkboxSel.getSelections().length === 0) {
        Ext.Msg.alert("提示", "您还没有选择，需要选择一个记录才能修改");
    }
    else if (checkboxSel.getSelections().length > 1) {
        Ext.Msg.alert("提示", "您选择了多个记录，只能选择一个记录进行修改");
    }
    else {
        if (win === null) {
            win = new Ext.Window({
                layout: 'form',
                width: 340,
                autoHeight: true,
                closeAction: 'hide',
                items: [inputForm]
            });
        }

        //alert(checkboxSel.getSelected().get('code'));
        type = 2;
        Ext.getCmp('input-form').getForm().loadRecord(checkboxSel.getSelected());
        //Ext.Msg.alert("a",Ext.getCmp("code"));
        //Ext.getCmp('code').readOnly=true;
        //Ext.getCmp('YesNo').setValue(checkboxSel.getSelected().get("ismine"));  
        //alert(str);
        //alert(Ext.getCmp('checkdate'));
        win.setTitle('修改' + theme);
        win.show();
        //Ext.getDom('win-title').innerHTML='修改监控设备信息';
    }
}

function remove() {
    if (checkboxSel.getSelections().length === 0) {
        Ext.Msg.alert("修改" + theme, "您还没有选择，需要先选择才能删除");
    }
    else {
        Ext.MessageBox.confirm('提示', '确实要删除所选的记录吗?', function(btn) {
            if (btn === 'yes') {
                var records = checkboxSel.getSelections();
                var jsonData = "";
                for (var i = 0, len = records.length; i < len; i++) {
                    var ss = records[i].get("id"); //这里为Grid数据源的Id列
                    if (i === 0)
                        jsonData = jsonData + ss;
                    else
                        jsonData = jsonData + "," + ss;
                }
                var conn = new Ext.data.Connection();
                var token1 = new Date().getTime();
                conn.request({
                    url: deleteServlet, //提交的删除地址
                    params: {token:token,token1:token1,ids: jsonData},
                    method: 'post',
                    scope: this,
                    callback: function(options, success, response) {
                        if (success) {
                            var messager = Ext.decode(response.responseText);
                            if (messager.success) {
                                Ext.MessageBox.alert("提示", "所选记录成功删除！");
                                store.reload(); //重新load数据   	
                                //   parent.Ext.getCmp('OrganTree').getNodeById(currentOrgCode).reload();
                            }
                            else {
                                Ext.Msg.alert('提示', messager.msg);
                            }
                        } else {
                            Ext.MessageBox.alert("提示", "所选记录删除失败！");
                        }
                    }
                });
            }
        });
    }
}
var checkboxSel = new Ext.grid.CheckboxSelectionModel();
var store;
function buildWindow() {
    //alert(Ext.getCmp("dateFrom"));
    //alert(dateFromField.value.format("Ymd"));
    store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName,
        autoLoad: true,
        baseParams: {dateFrom: dateFromField.value.format("Ymd"), dateTo: now.add(Date.DAY,1).format("Ymd"), start: 0, limit: _pageSize}
    });

    var grid = new Ext.grid.GridPanel({
        title: _title,
        store: store,
        sm: checkboxSel,
        columns: [
            checkboxSel,
            {header: fieldLabel[0], sortable: true, dataIndex: fieldName[0], hidden: true},
            {header: fieldLabel[1], width: 80, sortable: true, dataIndex: fieldName[1], hidden: true},
            {header: fieldLabel[2], width: 100, sortable: true, dataIndex: fieldName[2]},
            {header: fieldLabel[3], width: 100, sortable: true, dataIndex: fieldName[3]},
            {header: fieldLabel[4], width: 120, sortable: true, dataIndex: fieldName[4]},
            {header: fieldLabel[5], width: 200, sortable: true, dataIndex: fieldName[5]},
            {header: fieldLabel[6], width: 180, sortable: true, dataIndex: fieldName[6]},
            {header: fieldLabel[7], width: 120, sortable: true, dataIndex: fieldName[7], hidden: true},
            {header: fieldLabel[8], width: 180, sortable: true, dataIndex: fieldName[8]}

        ],
        loadMask: true,
        tbar: new Ext.Toolbar([
            {text: '新增', iconCls: 'icon-add', handler: addnew, hidden: 'true'},
            {text: '修改', iconCls: 'icon-edit', handler: update, hidden: 'true'},
            {text: '删除', iconCls: 'icon-delete', handler: remove, hidden: 'true'},
            //'-',
            dateFromField,
            lblTo,
            {
                xtype: 'datefield',
                format: 'Y年m月d日',
                id:"DateTo",
                width: 150,                
                value: now,
                listeners: {
                    select: function(c, date) {
                        store.baseParams.dateTo = date.add(Date.DAY,1).format('Ymd');
                    }
                }
            },
            {
                xtype: 'textfield',                
                emptyText: '输入模糊查询关键字'
                , id: 'Name'
            },
            '-',
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {
                    store.baseParams.name = Ext.getCmp("Name").getValue();                    
                    store.load({start:0,limit:_pageSize});
                }
            }
            //   {text: '用户角色分配', iconCls: 'icon-edit', handler: allocation}
        ]),
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: "目前没有" + theme + '信息'
        })
    });


    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            grid
        ]
    });
}
Ext.onReady(buildWindow);  